<?php
namespace Home\Model;
use Think\Model;

class ArticleModel extends Model{
    
    //验证
    protected $_validate = array(
      
        array('title','require','标题不能为空!'),
        array('content','require','内容不能为空!'),
        //array('classid','require','内容不能为空!'),
        //array('dateline','require','内容不能为空!'),
        //array('name','','帐号名称已经存在！',0,'unique',1), // 在新增的时候验证name字段是否唯一
        //array('value',array(1,2,3),'值的范围不正确！',2,'in'), // 当值不为空的时候判断是否在一个范围内
        //array('repassword','password','确认密码不正确',0,'confirm'), // 验证确认密码是否和密码一致
        //array('password','checkPwd','密码格式不正确',0,'function'), // 自定义函数验证密码格式
    );
    //完成
    protected $_auto= array(
        
        array('dateline','dateline',3,'callback'),
        array('attr','attr',3,'callback'),
        array('pic','upload',3,'callback'),
        array('seo_keywords','seo_keywords',3,'callback'),
        array('seo_desc','seo_desc',3,'callback'),
        array('tag','tag',3,'callback'),
        
        //array('status','1'),  // 新增的时候把status字段设置为1
        //array('password','md5',3,'function') , // 对password字段在新增和编辑的时候使md5函数处理
        //array('name','getName',3,'callback'), // 对name字段在新增和编辑的时候回调getName方法
        //array('update_time','time',2,'function'), // 对update_time字段在更新的时候写入当前时间戳
        
    );
    //seo_keywords
    public function seo_keywords(){
        
        $keywords = preg_replace("/(\n)|(\s)|(\t)|(\')|(')|(，)/",',',I('seo_keywords'));
        $keywords = explode(',', $keywords);
        $keywords = array_filter($keywords);
        $keywords = array_unique($keywords);
        $keywords = implode(',', $keywords);
        return keywords;
        
    }
    //tag
    public function tag(){
        
        $id = I('id');
        $tag1 = I('tag');
        $tag1 = preg_replace("/(\n)|(\s)|(\t)|(\')|(')|(，)/",',',$tag1);//分隔符转换
        $tag2 = I('_tag');
        //现有数据
        $tag1 = $tag1== null ? array() : explode(',', $tag1);
        //原始数据
        $tag2 = $tag2== null ? array() : explode(',', $tag2);
        if($tag1==null && $tag2=null){
            return "";
        }
        //虑空 去重复
        $tag1 = array_filter($tag1);
        $tag1 = array_unique($tag1);
        $dtag = D('tag');
        //查询tagid
        $arttag = $this->find($id);
        $art_tag = $arttag['tag'];
        $art_tag = $art_tag == null ? array() : explode('', $art_tag);
        //去{}
        if($art_tag != null){
            foreach ($art_tag as $vv){
                $art_tag_t[] = substr(substr($vv, 1), -1); 
            }
            $art_tag = $art_tag_t;
        }
        //正反对比
        $arrz = array_diff($tag1,$tag2);
        $arrf = array_diff($tag2,$tag1);
        //未变
        if($arrz==null && $arrf==null){
            $tag_id = $art_tag;
        }
        //减少
        if($arrz==null || $arrf!=null){
                foreach ($arrf as $k){
                    $data['name'] = $k;
                    $tagdata = $dtag->where($data)->find();
                    $where['id'] = $tagdata['id'];
                    $dtag->where($where)->setDec('tag_count');
                    $tagid[] = $tagdata['id'];
                }
                //取差
                $tag_id = array_diff($art_tag, $tagid);
                $tagid = array();//重置
                $art_tag = $tag_id;//增加初始化    
        }
        //增加
        if($arrf==null || $arrz!=null){
             foreach ($arrz as $v){
                 $data['name'] = $v;
                 $tagdata = $dtag->where($data)->find();
                 if($tagdata){//已存在数据库
                     $where['id'] = $tagdata['id'];
                     $dtag->where($where)->setInc('tag_count');
                     $tagid[] = $tagdata['id'];
                 }else{//新添加
                     $data['tag_count'] = 1;
                     $tagid[] = $dtag->add($data); 
                 } 
             }
             //取差
             $tag_id = array_diff($tagid, $art_tag);
             //合并
             $tag_id = array_merge($art_tag, $tag_id);
         }
         //加{}
         foreach ($tag_id as $vv){
             $tagtag[] = '{'.$vv.'}';
         }
        $tagtag = implode('', $tagtag);
        return $tagtag; 
    }
    //属性
    public function attr(){
        
        $attr_b = I('attr');
        $attr[0] = $attr_b[0]==1 ? 1 : 0;
        $attr[1] = $attr_b[1]==1 ? 1 : 0;
        $attr[2] = $attr_b[2]==1 ? 1 : 0;
        $attr = implode(',', $attr);
        return $attr;
        
    }
    //描述
    public function seo_desc(){
        
        if(I('seo_desc')!=null){
            return I('seo_desc');
        }else {
            $content =htmlspecialchars_decode(I('content'));
            $content = strip_tags($content);
            $content = substr($content, 0,200);
            return $content;
        }
      
    }
    //时间
    public function dateline(){
        
        if($_POST['dateline']!=null){
            $dateline = strtotime($_POST['dateline']);
        }else{
            $dateline = time();
        }
        
        return $dateline;
    }
    //上传
    public function upload(){

       if($_FILES['pic']['name']){
           
           $upload = new \Think\Upload();// 实例化上传类
           $upload->maxSize   =     3145728 ;// 设置附件上传大小
           $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
           $upload->rootPath  =     './Uploads/'; // 设置附件上传根目录
           $upload->savePath  =     'img/'.date('Ym',time()).'/'; // 设置附件上传（子）目录
           $upload->subName = array('date','d');//子目录创建方式
           $upload->saveName  = date('Ymd',time()).'_'.mt_rand();
           // 上传文件
           $info   =   $upload->uploadOne($_FILES['pic']);
           if(!$info) {// 上传错误提示错误信息
               //$this->error($upload->getError());
           }else{// 上传成功
           
               $picPath = './Uploads/'.$info['savepath'].$info['savename'];
               $image = new \Think\Image();
               $image->open($picPath);
               //设置大小
               $pic_size = $this->table('jq_info')->field('web_picsize')->find(1);
               $pic_size = explode(',', $pic_size['web_picsize']);
               //居中裁剪
               $image->thumb($pic_size[0],$pic_size[1],\Think\Image::IMAGE_THUMB_CENTER)->save($picPath);
           
               //$this->success('上传成功！');
           }
           return $info['savepath'].$info['savename'];
           
       }else {
           
           if($_POST['_pic']!=null){
               
               return $_POST['_pic'];
               
           }else{
               
               $content=$_POST['content'];
               $info=getPic($content);
               $picPath = '.'.$info;
               if(!$info==null){
                   $thumb='.'.$info.'_thumb.jpg';
                   $image = new \Think\Image();
                   $image->open($picPath);
                   //设置缩略图
                   $pic_size = $this->table('jq_info')->field('web_picsize')->find(1);
                   $pic_size = explode(',', $pic_size['web_picsize']);
                   $image->thumb($pic_size[0],$pic_size[1],\Think\Image::IMAGE_THUMB_CENTER)->save($thumb);
                   $imgpath = $info.'_thumb.jpg';
                   return substr($imgpath,9);//返回子目录路径
               }else{
                   $thumb='';
                   return $thumb;
               }
                 
           }
           
       }
        
    }
    //
    
    
 
}